package com.spring.app.util;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Future;

import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.spring.app.domain.BuyerData;
import com.spring.app.genpostagelabel.BuyerDetail;
import com.spring.app.genpostagelabel.ColumnIndexes;
import com.spring.app.genpostagelabel.Utils;

@Component
public class CsvFileUtil {
	public static final char COL_SEPARATOR='"';

	
	
	public Map readCsv(String fileName) throws Exception{
		
		FileInputStream file = new FileInputStream(new File("C:\\ebay",fileName));
        
		List<String> rows=IOUtils.readLines(file);
		List<BuyerData> buyerDetails=new ArrayList<BuyerData>();
		Map<String,BuyerData> buyerDetailsMap=new HashMap<String,BuyerData>();
	    List<String> duplicatesBuyerIds=new ArrayList<String>();
		int rc=1;
		
		for(String row:rows){
		String[] cols=row.split(",");
		if(cols.length>32 && !Utils.isEmpty(cols[0]) && !Utils.trim(cols[1],COL_SEPARATOR).equals("User Id")){
			BuyerData buyerDetail=new BuyerData();
			
			if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_SALESRECORD]))
		        buyerDetail.setSalesRecord(Utils.trim(cols[ColumnIndexes.COL_NO_SALESRECORD],COL_SEPARATOR));
		        
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_USER_ID]))
	        		buyerDetail.setUserId(Utils.trim(cols[ColumnIndexes.COL_NO_USER_ID],COL_SEPARATOR));
	        	
	        	if(!Utils.isEmpty(buyerDetailsMap.get(buyerDetail.getUserId())))
	        	{
	        		duplicatesBuyerIds.add(buyerDetail.getUserId());
	        		buyerDetail.setFullName(buyerDetailsMap.get(buyerDetail.getUserId()).getFullName());
	        		buyerDetail.setPhoneNo(buyerDetailsMap.get(buyerDetail.getUserId()).getPhoneNo());
	        		buyerDetail.setEmail(buyerDetailsMap.get(buyerDetail.getUserId()).getEmail());
	        		buyerDetail.setAddress1(buyerDetailsMap.get(buyerDetail.getUserId()).getAddress1());
	        		buyerDetail.setAddress2(buyerDetailsMap.get(buyerDetail.getUserId()).getAddress2());
	        		buyerDetail.setCity(buyerDetailsMap.get(buyerDetail.getUserId()).getCity());
	        		buyerDetail.setCountry(buyerDetailsMap.get(buyerDetail.getUserId()).getCountry());
	        		buyerDetail.setState(buyerDetailsMap.get(buyerDetail.getUserId()).getState());
	        		buyerDetail.setPostcode(buyerDetailsMap.get(buyerDetail.getUserId()).getPostcode());
	        		
	        	}else{
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_FULLNAME]))
			        buyerDetail.setFullName(Utils.trim(cols[ColumnIndexes.COL_NO_FULLNAME],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_PHONE_NO]))
	        		buyerDetail.setPhoneNo(Utils.trim(cols[ColumnIndexes.COL_NO_PHONE_NO],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_EMAIL]))
	        		buyerDetail.setEmail(Utils.trim(cols[ColumnIndexes.COL_NO_EMAIL],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_ADDRESS_1]))
			        buyerDetail.setAddress1(Utils.trim(cols[ColumnIndexes.COL_NO_ADDRESS_1],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_ADDRESS_2]))
			        buyerDetail.setAddress2(Utils.trim(cols[ColumnIndexes.COL_NO_ADDRESS_2],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_CITY]))
			        buyerDetail.setCity(Utils.trim(cols[ColumnIndexes.COL_NO_CITY],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_STATE]))
			        buyerDetail.setState(Utils.trim(cols[ColumnIndexes.COL_NO_STATE],COL_SEPARATOR));
	        	if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_POSTCODE]))
			        buyerDetail.setPostcode(Utils.trim(cols[ColumnIndexes.COL_NO_POSTCODE],COL_SEPARATOR));
		        if(!Utils.isEmpty(cols[ColumnIndexes.COL_NO_COUNTRY]))
			        buyerDetail.setCountry(Utils.trim(cols[ColumnIndexes.COL_NO_COUNTRY],COL_SEPARATOR));
	        	}
		        buyerDetailsMap.put(buyerDetail.getUserId(), buyerDetail);
		        buyerDetails.add(buyerDetail); 
		}
		
		}
		
	
		 
		/* Set<BuyerData> buyerDataSet = new TreeSet<BuyerData>(new BuyerDataComparator());
	     buyerDataSet.addAll(buyerDetails);
	        
	      buyerDetails.retainAll(buyerDataSet);*/
	      
	     //Sorting as per salesRecord   
		 Collections.sort(buyerDetails, new BuyerDataComparator());
		 Map m=new HashMap();
	      m.put("buyerDataList", buyerDetails);
	      m.put("repeatedBuyers", duplicatesBuyerIds);
		 
	return m;	    
	}
}
